package com.uusee.shipshape.gt.util;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.uusee.util.regex.RegexUtils;

public class RenameUtils {
	private static final String S_REG_1 = ".*?第\\s*?([0-9]+)\\s*?[季|部](.*)";
	private static final String S_REG_2 = ".*?第\\s*?([一|二|三|四|五|六|七|八|九|十|百]+)\\s*?[季|部](.*)";
	private static final String S_REG_3 = ".*?S(\\d+) ?(E.*)";
	private static final String[] S_REG = { S_REG_1, S_REG_2, S_REG_3};

	private static final String E_REG_1 = ".*?第\\s*?([0-9]+)\\s*?[集|回|话](.*)";
	private static final String E_REG_2 = ".*?第\\s*?([一|二|三|四|五|六|七|八|九|十|百]+)\\s*?[集|回|话](.*)";
	private static final String E_REG_3 = ".*?([0-9][0-9]{0,2})(.*)";
	private static final String[] E_REG = { E_REG_1, E_REG_2, E_REG_3};
	private static final String[] E_REGS_2 = { E_REG_1, E_REG_2};

	private static final String P_REG_1 = ".*?[A-I].*";
	private static final String P_REG_2 = ".*?[1-7].*";
	private static final String[] P_REG = { P_REG_1, P_REG_2 };

	private static Map<String, Integer> numMap = new HashMap<String, Integer>();
	static {
		numMap.put("一", Integer.valueOf("1"));
		numMap.put("二", Integer.valueOf("2"));
		numMap.put("三", Integer.valueOf("3"));
		numMap.put("四", Integer.valueOf("4"));
		numMap.put("五", Integer.valueOf("5"));
		numMap.put("六", Integer.valueOf("6"));
		numMap.put("七", Integer.valueOf("7"));
		numMap.put("八", Integer.valueOf("8"));
		numMap.put("九", Integer.valueOf("9"));
		numMap.put("十", Integer.valueOf("10"));
		numMap.put("百", Integer.valueOf("100"));
	}

	public static int chNumToNum(String s) {
		int num = 0;
		if (!NumberUtils.isNumber(s)) {
			s = chNumsToNum(s)+"";
		}
		while (s.startsWith("0")) {
			s = StringUtils.removeStart(s, "0");
		}
		num = NumberUtils.toInt(s, 0);
		return num;
	}

	private static int chNumsToNum(String chNums) {
		int num = 0;
		try {
			if (StringUtils.isNotEmpty(chNums)) {
				int len = chNums.length();
				
				int lastNum = 0;
				for (int i = 0; i < len; i++) {
					String chNum = chNums.substring(i,i+1);
					if (!numMap.containsKey(chNum)) {
						continue;
					}
					int iNum = numMap.get(chNum);
					if (iNum==100) {
						num = (num-lastNum)+lastNum*100;
						lastNum = iNum;
					} else if (iNum==10&&lastNum!=0) {
						num = (num-lastNum)+lastNum*10;
						lastNum = iNum;
					} else {
						num = num+iNum;
						lastNum = iNum;
					}
				}
			}
		} catch (Exception e) {
			num = 0;
		}
		return num;
	}
	
	public static String format(int n,String f) {
		String s = "";
		try {
			DecimalFormat df = new DecimalFormat(f);
			s = df.format(n);
		} catch (Exception e) {
			
		}

		return s;
	}
	
	public static String rename(String name,String title) {
		String newName = "";
		try {
			int start = title.indexOf(name);
			String subNewTitle = title;
			if (start>=0) {
				subNewTitle = title.substring(start+name.length());
			}
			int season = getSeason(subNewTitle);
			String sSeason = format(season, "00");
			int episode = getEpisode(subNewTitle);
			String sEpisode = format(episode, "00");
			String paragraph = getParagraph(subNewTitle);
			
			if ( episode != 0 ) {
				StringBuffer tSb = new StringBuffer();
				tSb.append(name);
				tSb.append(" ");
				int nameSeason = getSeason(name);
				if (season!=0 && nameSeason==0) {
					tSb.append("第");
					tSb.append(sSeason);
					tSb.append("季");
					tSb.append(" ");
				}
				tSb.append("E");
				tSb.append(sEpisode);
				if (StringUtils.isNotEmpty(paragraph)) {
					tSb.append("-");
					tSb.append(paragraph);
				}
				newName = tSb.toString();
			}
		} catch (Exception e) {
			
		}

		return newName;
	}

	public static int getSeason(String text) {
		String sSeason = "";
		String reg = getReg(text, S_REG);
		if (StringUtils.isNotEmpty(reg)) {
			sSeason = RegexUtils.getValue(reg, text, 1);
		}
		int iSeason = chNumToNum(sSeason);
		return iSeason;
	}

	public static int getEpisode(String text) {
		String episodeSection = getEpisodeSection(text);
		String reg = getReg(episodeSection, E_REG);
		String sEpisode = "";
		if (StringUtils.isNotEmpty(reg)) {
			sEpisode = RegexUtils.getValue(reg, episodeSection, 1);
		}
		int iEpisode = chNumToNum(sEpisode);
		return iEpisode;
	}

	public static String getParagraph(String text) {
		String episodeSection = getEpisodeSection(text);
		String paragraphSection = getParagraphSection(episodeSection);
		String reg = getReg(paragraphSection, P_REG);
		String sParagraph = "";
		if (StringUtils.isNotEmpty(reg)) {
			sParagraph = RegexUtils.getValue(reg, paragraphSection, 1);
		}
		return sParagraph;
	}

	public static String getReg(String text, String[] regs) {
		String reg = "";
		for (String r : regs) {
			if (RegexUtils.isMatch(r, text)) {
				reg = r;
				break;
			}
		}
		return reg;
	}

	private static String getEpisodeSection(String title) {
		String episodeSection = title;
		String reg = getReg(title, S_REG);
		if (StringUtils.isNotEmpty(reg)) {
			episodeSection = RegexUtils.getValue(reg, title, 2);
		}
		String episodeReg = getReg(episodeSection, E_REGS_2);
		if (StringUtils.isEmpty(episodeReg)) {
			String r = ".*?(\\d+)(.*)";
			String numStr1 = RegexUtils.getValue(r, episodeSection, 1);
			if (numStr1.length()>=4) {
				episodeSection = RegexUtils.getValue(r, episodeSection, 2);
			} else if (numStr1.length()<=1) {
				String s2 = RegexUtils.getValue(r, episodeSection, 2);
				String numStr2 = RegexUtils.getValue(r, s2, 1);
				if (numStr2.length()==2||numStr2.length()==3) {
					episodeSection = s2;
				}
			}
		}
		return episodeSection;
	}

	private static String getParagraphSection(String episodeSection) {
		String paragraphSection = "";
		String eReg = getReg(episodeSection, E_REG);
		if (StringUtils.isNotEmpty(eReg)) {
			paragraphSection = RegexUtils.getValue(eReg, episodeSection, 2);
		}
		return paragraphSection;
	}

	public static void main(String[] args) {
		
		int start = "womw中国人文秘".indexOf("中国");
		String subNewTitle = "";
		if (start>=0) {
			subNewTitle = "womw中国人文秘".substring(start+"中国".length());
		}
		System.out.println(subNewTitle);
		

	}

}
